<script setup>
  import { addTeacherService, editTeacherService } from '@/api/user.js'
  import { ref } from 'vue'
  const DrawerVisible = ref(false)
  const defaultForm = {
    openId: '',
    code: '',
    name: '',
    phone: '',
    office: '',
    role: '',
    mentor: '',
    charger: '',
    password: '',
  }
  const emit = defineEmits(['success'])
  const formModel = ref({ ...defaultForm })
  const onSubmit = async () => {
    if (formModel.value._id) {
      await ElMessageBox.confirm('你确认编辑吗', '温馨提示', {
        type: 'warning',
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      })
      await editTeacherService({ id: formModel.value._id, teacher: formModel.value })
      ElMessage.success('编辑成功')
      DrawerVisible.value = false
      emit('success', 'edit')
    } else {
      await ElMessageBox.confirm('你确认添加吗', '温馨提示', {
        type: 'warning',
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      })
      await addTeacherService({ user: formModel.value })
      ElMessage.success('添加成功')
      DrawerVisible.value = false
      emit('success', 'add')
    }
    DrawerVisible.value = false
  }
  const open = async (row) => {
    DrawerVisible.value = true
    formModel.value = { ...row }
  }

  defineExpose({
    open
  })
</script>
<template>
  <el-drawer v-model="DrawerVisible" :title="formModel._id ? '编辑教师信息' : '添加教师'" direction="rtl" size="50%">

    <el-form label-width="100px" :model="formModel" ref="form">
      <el-form-item label="教工工号">
        <el-input v-model="formModel.code" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="教工姓名">
        <el-input v-model="formModel.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机">
        <el-input v-model="formModel.phone" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="办公室">
        <el-input v-model="formModel.office" placeholder="请输入办公室"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formModel.role" class="m-2" placeholder="请选择角色" style="width: 240px">
          <el-option :key="0" label='教师' :value="0" />
          <el-option :key="1" label='管理员' :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否是导师">
        <el-radio v-model="formModel.mentor" :label="true">是</el-radio>
        <el-radio v-model="formModel.mentor" :label="false">否</el-radio>
      </el-form-item>
      <el-form-item label="是否是班主任">
        <el-radio v-model="formModel.charger" :label="true">是</el-radio>
        <el-radio v-model="formModel.charger" :label="false">否</el-radio>
      </el-form-item>

      <el-form-item>
        <el-button @click="onSubmit" type="primary">提交</el-button>
        <el-button @click="DrawerVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>